<template>
  <div>
    <!-- 增补订单提报 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTable
        ref="table"
        columnsRef="orderSubmissions"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        :onSyncTableData="tableChange"
        :isSelectColumn="false"
        :onColumnsChange="(columns) => (this.columns = columns)"
      >
        <template slot="controls" slot-scope="props">
          <multiuse-button size="small" type="primary" @click="addHandle(1)">新 建</multiuse-button>
          <multiuse-button size="small" type="primary" @click="addHandle(2)">导 出</multiuse-button>
        </template>
      </FilterTable>
    </Spin>

    <Drawer :visible.sync="addVisible" :title="'订单信息'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" width="80%">
      <addModel :rowData="formData" @close="addcloseHandler" />
    </Drawer>
    <Drawer :visible.sync="detailVisible" :title="'订单信息'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" width="80%">
      <detailModel :rowData="formData" @close="detailcloseHandler" />
    </Drawer>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { getList, exportList, deleteOrderById, exporturl } from '@/api/dealerSaleP/KeyCustomers/OrderSubmission/index.js';
import addModel from './addModel';
import detailModel from './detailModel';
export default {
  name: 'zjTemplateDistribution',
  mixins: [dictionary],
  components: {
    addModel,
    detailModel,
  },
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: getList,
      fetchParams: {
        searchType: 1, //查询类型 1为提报查询 2为审核查询列表 3为订货单主机厂查询 4为订货单经销商查询
        saveType: 'DLR', //DLR为经销商查询 OEM为厂端查询
        majorType: 1 //是否大客户 1为非大客户查询 2为大客户查询 3为全部查询
      },
      tableList: [],
      formData: {},
      exportLoading: false,
      addVisible: false,
      detailVisible: false,
    };
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
  },
  methods: {
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '订单编号',
          fieldName: 'orderNo',
          placeholder: '请输入',
        },
        {
          type: 'SELECT',
          label: '订单状态',
          fieldName: 'orderStatus',
          placeholder: '请选择',
          itemList: this.createDictList('2104'),
        },
        {
          type: 'RANGE_DATE',
          label: '订单年月',
          fieldName: 'startDate|endDate',
          placeholder: '请选择',
          initialValue: [],
          dateType: 'exactdaterange',
        },
        {
          type: 'SELECT',
          label: '运输方式',
          fieldName: 'transportType',
          placeholder: '全部',
          itemList: this.createDictList('2409'),
        },
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: (props) => {
            return <span>{props.row.index + 1}</span>;
          },
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row, 1)} >
                  编辑
                </multiuse-button>
                <el-divider direction="vertical" />
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row, 2)}>
                  详情
                </multiuse-button>
                <el-divider direction="vertical" />
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row, 3)} >
                  删除
                </multiuse-button>
              </div>
            );
          },
        },
        {
          title: '订单编号',
          dataIndex: 'orderNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'center',
          width: 200,
        },
        {
          title: '订单类型',
          dataIndex: 'orderType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('2105'),
          render: (p) => {
            return <span>{this.createDictText(p.row.orderType, '2105')}</span>;
          },
        },
        {
          title: '订单细分类型',
          dataIndex: 'orderDetailName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '订单状态',
          dataIndex: 'orderStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('2104'),
          render: (p) => {
            return <span>{this.createDictText(p.row.orderStatus, '2104')}</span>;
          },
        },
        {
          title: '订单年月',
          dataIndex: 'orderYearMonth',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '订单紧急度',
          dataIndex: 'emergencyType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('2101'),
          render: (p) => {
            return <span>{this.createDictText(p.row.emergencyType, '2101')}</span>;
          },
        },
        {
          title: '运输方式',
          dataIndex: 'transportType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('2409'),
          render: (p) => {
            return <span>{this.createDictText(p.row.transportType, '2409')}</span>;
          },
        },
        {
          title: '收车地址',
          dataIndex: 'orderAddress',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '送达城市',
          dataIndex: 'orderCity',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '联系人',
          dataIndex: 'linkMan',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '联系方式',
          dataIndex: 'linkPhone',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '创建人',
          dataIndex: 'createdName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '创建日期',
          dataIndex: 'createdAt',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        }
      ];
    },
    changeHandle(params) {
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    tableChange(val) {
      val.map((x) => {
      });
    },
    async addHandle(type) {
      if (type === 1) {
        // 新增
        this.formData = null;
        this.addVisible = true;
      } else {
        // 导出
        let json = Object.assign({}, { CODE: 'CODE_0000034' }, this.fetchParams);
        let res = await exporturl(json);
        if (res.resultCode === 200) {
          this.$router.push('/admin/auth/oem/downloadCenter');
        }
      }
    },
    async editHandle(data, type) {
      if (type === 1) {
        // 编辑
        if(data.orderStatus !== 21041001){
          this.$notify({
            title: '提示',
            message: '新建状态的订单才允许修改',
            type: 'warning',
          });
          return;
        }
        this.formData = data;
        this.addVisible = true;
      } else if (type === 2) {
        // 详情
        this.formData = data;
        this.detailVisible = true;
      } else {
        // 删除
        if(data.orderStatus !== 21041001){
          this.$notify({
            title: '提示',
            message: '新建状态的订单才允许删除',
            type: 'warning',
          });
          return;
        }
        await this.$confirm('是否确定取消?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(async () => {
            let res = await deleteOrderById({ orderId: data.orderId });
            if (res.resultCode === 200) {
              this.$notify({
                title: '成功',
                message: '取消成功',
                type: 'success',
              });
              this.fetchParams = Object.assign({}, this.fetchParams);
            }
          }).catch(() => {});
      }
    },
    addcloseHandler(val) {
      this.addVisible = false;
      if (val) {
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
    },
    detailcloseHandler() {
      this.detailVisible = false;
    }
  }
};
</script>
